package com.ssm.platform.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.ssm.basis.model.util.BeanUtil;
import com.ssm.basis.service.exception.CustomException;
import com.ssm.basis.service.office.ExcelUtil;
import com.ssm.platform.mapper.UsersMapper;
import com.ssm.platform.pojo.domain.UsersDo;
import com.ssm.platform.pojo.param.UsersParam;
import com.ssm.platform.pojo.query.UsersQuery;
import com.ssm.platform.pojo.vo.UsersVo;
import com.ssm.platform.service.IUsersService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Set;

import static com.ssm.basis.service.exception.CustomExceptionEnum.PASSWORD_EMPTY_EXCEPTION;

@Service
@RequiredArgsConstructor
public class UsersServiceImpl extends ServiceImpl<UsersMapper, UsersDo> implements IUsersService {
    final UsersMapper usersMapper;
    final PasswordEncoder passwordEncoder;
    final ApplicationContext applicationContext;

    @Override
    public UsersVo post(UsersParam param) {
        String password = param.getPassword();
        if (!StringUtils.hasLength(password)) {
            throw new CustomException(PASSWORD_EMPTY_EXCEPTION);
        }

        param.setPassword(passwordEncoder.encode(password));
        UsersDo usersDo = BeanUtil.copyProperties(param, UsersDo::new);
        if (SqlHelper.retBool(usersMapper.insert(usersDo))) {
            return BeanUtil.copyProperties(usersDo, UsersVo::new);
        }
        throw new CustomException("保存失败！");
    }

    @Override
    public Boolean delete(Set<String> usernames) {
        return SqlHelper.retBool(usersMapper.deleteBatchIds(usernames));
    }

    @Override
    public UsersVo put(UsersParam param) {
        String password = param.getPassword();
        if (StringUtils.hasLength(password)) {
            param.setPassword(passwordEncoder.encode(password));
        }

        UsersDo usersDo = BeanUtil.copyProperties(param, UsersDo::new);
        if (SqlHelper.retBool(usersMapper.updateById(usersDo))) {
            return BeanUtil.copyProperties(usersDo, UsersVo::new);
        }
        throw new CustomException("修改失败！");
    }

    @Override
    public IPage<UsersVo> get(UsersQuery query) {
        // 不进行 count sql 优化，解决 MP 无法自动优化 SQL 问题，这时候你需要自己查询 count 部分
        // page.setOptimizeCountSql(false);
        // 当 total 为小于 0 或者设置 setSearchCount(false) 分页插件不会进行 count 查询
        // 要点!! 分页返回的对象与传入的对象是同一个
        return page(query, Wrappers.emptyWrapper()).convert(d -> BeanUtil.copyProperties(d, UsersVo::new));
    }

    @Override
    public byte[] export(UsersQuery query) {
        query.setCurrent(1L);
        query.setSize(Long.MAX_VALUE);
        IUsersService usersService = applicationContext.getBean(IUsersService.class);
        IPage<UsersVo> usersVoPage = usersService.get(query);

        return ExcelUtil.createExcelBytes("用户列表", usersVoPage.getRecords());
    }
}